﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using MINERP.InfoConfig.Controller;
using MINERP.DAL;

namespace MINERP.InfoConfig
{
    public partial class FrmProductTypeManager : FrmMDIChildFormTemplate
    {
        private ProductTypeController _controller = new ProductTypeController();
        tb_ProductType _productType = null;
        private static FrmProductTypeManager instance = null;
        private FrmProductTypeManager()
        {
            InitializeComponent();
        }

        public static FrmProductTypeManager CreateInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmProductTypeManager();
                }
                return instance;
            }
        }

        private void FrmProductTypeManager_Load(object sender, EventArgs e)
        {
            _controller.Grid = gridControlProductType;
            _controller.View = gridViewProductType;
            _controller.GetData();
        }

        private void gridViewProductType_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _productType = (tb_ProductType)gridViewProductType.GetFocusedRow();
            if (_productType != null)
            {
                txtProductTypeCode.Text = _productType.ProductTypeCode.ToString();
                txtRemark.Text = _productType.Remark;
                txtTypeName.Text = _productType.TypeName;
            }
        }
        //添加
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                txtTypeName.Focus();
                return;
            }
            if (CheckData())
            {
                tb_ProductType type = new tb_ProductType();
                type.ProductTypeCode = Convert.ToInt32(txtProductTypeCode.Text);
                type.Remark = txtRemark.Text;
                type.TypeName = txtTypeName.Text;
                _controller.Add(type);

                //link datagrid
                _controller.Grid = gridControlProductType;
                _controller.View = gridViewProductType;
                _controller.GetData();

                PrepareAdd(false);
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //更新
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtTypeName.Focus();
                return;
            }
            if (CheckData())
            {
                tb_ProductType type = _controller.GetProductTypeObj(_productType.ProductTypeId);
                type.ProductTypeCode = Convert.ToInt32(txtProductTypeCode.Text);
                type.Remark = txtRemark.Text;
                type.TypeName = txtTypeName.Text;
                _controller.Update();


                //refresh datagrid
                _controller.Grid = gridControlProductType;
                _controller.View = gridViewProductType;
                _controller.GetData();
                PrepareUpdate(false);

                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 
        }
        //删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                tb_ProductType type = _controller.GetProductTypeObj(_productType.ProductTypeId);
                _controller.Delete(type);

                //refresh datagrid
                _controller.Grid = gridControlProductType;
                _controller.View = gridViewProductType;
                _controller.GetData();
            }
        }
        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtTypeName.Enabled = false;
            txtRemark.Enabled = false;
            txtProductTypeCode.Enabled = false;

            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnAdd.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
        }

        /// <summary>
        /// 检查数据完整性

        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtTypeName.Text == string.Empty)
            {
                txtTypeName.Focus();
                MessageBox.Show("产品类型名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtRemark.Text == string.Empty)
            {
                txtRemark.Focus();
                MessageBox.Show("备注不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (txtProductTypeCode.Text == string.Empty)
            {
                txtProductTypeCode.Focus();
                MessageBox.Show("产品类型代号不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置

        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {
            txtTypeName.Enabled = bl;
            txtRemark.Enabled = bl;
            txtProductTypeCode.Enabled = bl;

            txtTypeName.Text = string.Empty;
            txtRemark.Text = string.Empty;
            txtProductTypeCode.Text = string.Empty;


            if (bl)
            {
                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置

        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {
            txtTypeName.Enabled = bl;
            txtRemark.Enabled = bl;
            txtProductTypeCode.Enabled = bl;

            if (bl)
            {

                btnUpdate.Text = "保存";
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnUpdate.Text = "更新";
                btnAdd.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        private void FrmProductTypeManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }
    }
}
